package com.cskaoyan.market.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cskaoyan.market.db.domain.MarketRegion;
import com.cskaoyan.market.db.domain.MarketRegionExample;
import com.cskaoyan.market.db.mapper.MarketRegionMapper;
import com.cskaoyan.market.db.vo.MarketRegionVo;
import com.cskaoyan.market.admin.service.AdminRegionService;
import com.cskaoyan.market.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminRegionServiceImpl implements AdminRegionService {
    @Autowired
    private MarketRegionMapper regionMapper;

    /**
     * 行政区域查询
     * @return List<MarketRegionVo> 分级列表
     */
    @Override
    public List<MarketRegionVo> list() {
        // 创建返回对象
        List<MarketRegionVo> marketRegionVos = new ArrayList<>();
        // 获取映射
        
        
        // 创建查询一类实例对象
        MarketRegionExample firstRegionExample = new MarketRegionExample();
        firstRegionExample.or().andTypeEqualTo(((byte) 1));
        List<MarketRegion> firstRegions = regionMapper.selectByExample(firstRegionExample);
        // 创建查询二类实例对象
        MarketRegionExample secondRegionExample = new MarketRegionExample();
        secondRegionExample.or().andTypeEqualTo(((byte) 2));
        List<MarketRegion> secondRegions = regionMapper.selectByExample(secondRegionExample);
        // 创建查询三类实例对象
        MarketRegionExample thirdRegionExample = new MarketRegionExample();
        thirdRegionExample.or().andTypeEqualTo(((byte) 3));
        List<MarketRegion> thirdRegions = regionMapper.selectByExample(thirdRegionExample);
        // 三层循环嵌套
        for (MarketRegion firstRegion : firstRegions) {
            MarketRegionVo marketRegionVo = new MarketRegionVo();
            marketRegionVo.setId(firstRegion.getId());
            marketRegionVo.setName(firstRegion.getName());
            marketRegionVo.setType(1);
            marketRegionVo.setCode(firstRegion.getCode());
            marketRegionVo.setChildren(new ArrayList<>());
            for (MarketRegion secondRegion : secondRegions) {
                if (!secondRegion.getPid().equals(firstRegion.getId())){
                    continue;
                }
                MarketRegionVo marketRegionVo2 = new MarketRegionVo();
                marketRegionVo2.setId(secondRegion.getId());
                marketRegionVo2.setName(secondRegion.getName());
                marketRegionVo2.setType(2);
                marketRegionVo2.setCode(secondRegion.getCode());
                marketRegionVo2.setChildren(new ArrayList<>());
                for (MarketRegion thirdRegion : thirdRegions) {
                    if (!thirdRegion.getPid().equals(secondRegion.getId())){
                        continue;
                    }
                    MarketRegionVo marketRegionVo3 = new MarketRegionVo();
                    marketRegionVo3.setId(thirdRegion.getId());
                    marketRegionVo3.setName(thirdRegion.getName());
                    marketRegionVo3.setType(3);
                    marketRegionVo3.setCode(thirdRegion.getCode());
                    marketRegionVo3.setChildren(new ArrayList<>());
                    // 三级并入二级
                    marketRegionVo2.getChildren().add(marketRegionVo3);
                }
                // 二级并入一级
                marketRegionVo.getChildren().add(marketRegionVo2);
            }
            // 一级并入列表
            marketRegionVos.add(marketRegionVo);
        }
        return marketRegionVos;
    }
}
